package com.seckill.search.controller;

import com.alibaba.fastjson.JSON;
import com.seckill.goods.pojo.Sku;
import com.seckill.search.pojo.Page;
import com.seckill.search.pojo.SkuInfo;
import com.seckill.search.service.SkuInfoService;
import com.seckill.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/*****
 * @Author: http://www.itheima.com
 * @Project: seckillproject
 * @Description: com.seckill.search.controller.SearchController
 **/
@RestController
@RequestMapping(value = "/search")
public class SearchController {

    @Autowired
    private SkuInfoService skuInfoService;
    /****
     * 分页查询秒杀商品
     */
    @GetMapping
    public Page<SkuInfo> search(@RequestParam(required = false)Map<String,String> searchMap) throws IOException {
        return skuInfoService.search(searchMap);
    }

    /***
     * 增量操作  ->删除索引   type=3
     *          ->修改索引   type=2
     *          ->添加索引   type=1
     */
    @PostMapping(value = "/modify/{type}")
    public Result modify(@PathVariable(value = "type")Integer type,@RequestBody SkuInfo skuInfo){
        //索引更新
        skuInfoService.modify(type,skuInfo);
        return Result.ok();
    }

    /***
     * 增量操作
     */
    @PostMapping(value = "/addIndex")
    public Result addIndex(@RequestBody Sku sku) throws IOException {
        //索引更新
        skuInfoService.addIndex(sku);
        return Result.ok();
    }

    /***
     * 增量操作
     */
    @PostMapping(value = "/batch/addIndex")
    Result batchAddIndex(@RequestBody List<Sku> skus) throws IOException{
        skuInfoService.batchAddIndex(skus);
        return Result.ok();
    }

    /***
     * 删除操作
     */
    @DeleteMapping(value = "/{id}")
    public Result deleteById(@PathVariable("id")String id) throws IOException {
        //索引更新
        skuInfoService.deleteById(id);
        return Result.ok();
    }

    /***
     * 修改Sku
     * @param type
     * @param sku
     * @return
     */
    @PostMapping(value = "/modify/sku/{type}")
    public Result modifySku(@PathVariable(value = "type")Integer type,@RequestBody Sku sku){
        //索引更新
        SkuInfo skuInfo = JSON.parseObject(JSON.toJSONString(sku),SkuInfo.class);
        skuInfoService.modify(type,skuInfo);
        return Result.ok();
    }

    @GetMapping(value = "/del/all")
    void deleteAll() throws IOException {
        skuInfoService.deleteAll();
    }

    @GetMapping(value = "/clear")
    void clear() throws IOException {
        skuInfoService.clear();
    }

    /***
     * 批量导入
     */
    @GetMapping(value = "/add/all")
    public Result addAll() throws IOException {
        //批量导入
        skuInfoService.addAll();
        return Result.ok();
    }


    /***
     * 批量导入
     */
    @GetMapping(value = "/create/index/{indexName}")
    public Result createIndex(@PathVariable(value = "indexName")String indexName) throws IOException {
        //创建索引库
        skuInfoService.createIndex(indexName);
        return Result.ok();
    }
}
